<template>
  <div class="header">
    <div class="bread">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
        <el-breadcrumb-item v-for="item in breadcrumbs" :to="item.path">{{ item.meta.title }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="info" v-if="userInfo">
      <!-- <span class="welcome-text">欢迎 {{ userInfo.account }} 登录v</span> -->
      <el-dropdown>
        <span class="el-dropdown-link">
          欢迎 {{ userInfo.account }} 登录
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="goToAccountCenter">个人中心</el-dropdown-item>
            <el-dropdown-item @click="logout">注销登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <el-avatar :size="40" :src="serverUrl + userInfo.imgUrl" />
    </div>
  </div>
</template>

<script setup>
import { ref ,computed,watch} from 'vue'
import { accountInfo } from '@/api/account'
import { serverUrl } from '@/utils/common';
import { useRouter,useRoute } from 'vue-router';
import {userStore} from '@/stores/userStore'

const store = userStore()
const router=useRouter();
const route=useRoute()
const breadcrumbs=computed(()=>{
  let  matched = route.matched.filter(item=>{
      return item.meta.unShow !==true
  })
  return matched;
})


const user = JSON.parse(sessionStorage.getItem('user'))
const userInfo = ref()

const getAccountInfo = async () => {
  let res = await accountInfo({ id: user.id })
  userInfo.value = res.accountInfo;
}
getAccountInfo()

watch(()=>store.userInfoState,(newVal,oldVal)=>{
  getAccountInfo()
})

// 新增方法：跳转到个人中心
const goToAccountCenter = () => {
  router.push('/accountCenter')
}

// 新增方法：注销登录
const logout = () => {
  sessionStorage.removeItem('user')
  router.push('/login')
}
</script>

<style scoped>
.header {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  padding: 0 20px;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.bread {
  display: flex;
  align-items: center;
}

.info {
  display: flex;
  align-items: center;
  margin-right: 30px;
}

.welcome-text {
  font-size: 14px;
  color: #606266;
}

.logout-link {
  color: #409eff;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.3s;
}

.logout-link:hover {
  color: #66b1ff;
}

:deep(.el-breadcrumb) {
  font-size: 14px;
}

:deep(.el-breadcrumb__inner) {
  color: #909399;
  font-weight: normal;
}

:deep(.el-breadcrumb__inner.is-link) {
  color: #606266;
  transition: color 0.3s;
}

:deep(.el-breadcrumb__inner.is-link:hover) {
  color: #409eff;
}

:deep(.el-breadcrumb__separator) {
  color: #c0c4cc;
}

.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}

.el-dropdown {
  margin-right: 10px;
}
</style>